package me.atbar.moonlib.builder;

import lombok.NonNull;
import org.bukkit.Bukkit;
import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarFlag;
import org.bukkit.boss.BarStyle;
import org.bukkit.boss.BossBar;
import org.bukkit.entity.Player;

/**
 * @author Administrator
 */
public class BossBarBuilder {

    private final BossBar bossBar;

    public BossBarBuilder(@NonNull String title, BarColor color, BarStyle style, BarFlag flag) {
        this.bossBar = Bukkit.createBossBar(title, color, style, flag);
    }

    public BossBarBuilder setColor(BarColor color) {
        bossBar.setColor(color);
        return this;
    }

    public BossBarBuilder setProgess(double progress) {
        bossBar.setProgress(progress);
        return this;
    }

    public BossBarBuilder setTitle(String title){
        bossBar.setTitle(title);
        return this;
    }

    public BossBarBuilder setStyle(BarStyle style) {
        bossBar.setStyle(style);
        return this;
    }

    public BossBarBuilder show(boolean visible) {
        bossBar.setVisible(visible);
        return this;
    }

    public BossBarBuilder addFlags(BarFlag flag) {
        bossBar.addFlag(flag);
        return this;
    }

    public BossBarBuilder addPlayer(Player player) {
        bossBar.addPlayer(player);
        return this;
    }

    public BossBarBuilder removeFlags(BarFlag flag) {
        if(!bossBar.hasFlag(flag)) {
            return this;
        }
        bossBar.removeFlag(flag);
        return this;
    }

    public BossBarBuilder removePlayer(Player player) {
        if(!bossBar.getPlayers().contains(player)) {
            return this;
        }
        bossBar.removePlayer(player);
        return this;
    }

    public BossBarBuilder addAll() {
        Bukkit.getOnlinePlayers().forEach((bossBar::addPlayer));
        return this;
    }

    public BossBarBuilder removeAll() {
        bossBar.removeAll();
        return this;
    }

    public BossBar build() {
        return this.bossBar;
    }

}
